package com.puman.watchtrade.util;
import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

public class MyViewPager extends ViewPager {
	
	 /** ����ʱ���µĵ� **/
    PointF downP = new PointF();
    /** ����ʱ��ǰ�ĵ� **/
    PointF curP = new PointF(); 
    
    OnSingleTouchListener onSingleTouchListener;
    private float mLastX;
    private float mLastY;
    private boolean isNeed = false;
    
    
    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyViewPager(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    	 switch (ev.getAction()) {
         case MotionEvent.ACTION_DOWN:
        	     Log.i("liuwenjie", "onInterceptTouchEvent ACTION_DOWN");
                 mLastX = ev.getX();
                 mLastY = ev.getY();
                 break;
         case MotionEvent.ACTION_MOVE:
        	     Log.i("liuwenjie", "onInterceptTouchEvent ACTION_MOVE");
                 float absx = Math.abs(ev.getX()-mLastX);
                 float absy = Math.abs(ev.getY()-mLastY);
                 mLastX = ev.getX();
                 mLastY = ev.getY();
                 Log.i("liuwenjie", absy+"onInterceptTouchEvent ACTION_MOVE"+absx);
                 if (Math.abs(absx)>10&&absx>absy) {     
                	 //����Ǻ��Ż�������ΪACTION_DOWN���¼����ӿؼ�listview����ˣ��������ˣ���ô��Ҫ���²���һ��ACTION_DOWN��
                	 //�������һ�������¼�
                 	  isNeed = true;
//                	  onTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, curP.x,curP.y, 0));
                	  Log.i("liuwenjie", "onInterceptTouchEvent ACTION_MOVE leftandright");
                      return true;
                 } else 
                	 return false;

         }
    	  return false;
    }
  
    public boolean dispatchTouchEvent(MotionEvent ev){
    	
    	getParent().requestDisallowInterceptTouchEvent(true);
    	return super.dispatchTouchEvent(ev);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        // TODO Auto-generated method stub
        //ÿ�ν���onTouch�¼�����¼��ǰ�İ��µ����
        curP.x = arg0.getX();
        curP.y = arg0.getY();
    	Log.i("liuwenjie", "onTouchEvent"+arg0.getAction());
        if(arg0.getAction() == MotionEvent.ACTION_DOWN){
        	Log.i("liuwenjie", "onTouchEvent ACTION_DOWN");
            //��¼����ʱ������
            //�мǲ����� downP = curP �������ڸı�curP��ʱ��downPҲ��ı�
            downP.x = arg0.getX();
            downP.y = arg0.getY();
            isNeed = false;
            //�˾������Ϊ��֪ͨ��ĸ�ViewPager���ڽ��е��Ǳ��ؼ��Ĳ�������Ҫ���ҵĲ������и���
        }
        
        //�ж��Ƿ���Ҫģ��һ��ACTION_DOWN�¼�
        if(isNeed)
        {
        	 onTouchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, curP.x,curP.y, 0));
        }

        if(arg0.getAction() == MotionEvent.ACTION_MOVE){
            //�˾������Ϊ��֪ͨ��ĸ�ViewPager���ڽ��е��Ǳ��ؼ��Ĳ�������Ҫ���ҵĲ������и���
        	Log.i("liuwenjie", "onTouchEvent ACTION_MOVE");
        }

        if(arg0.getAction() == MotionEvent.ACTION_CANCEL){
        	Log.i("liuwenjie", "onTouchEvent ACTION_CANCEL");
        }

        if(arg0.getAction() == MotionEvent.ACTION_UP){
            //��upʱ�ж��Ƿ��º����ֵ����Ϊһ����
            //�����һ���㣬��ִ�е���¼����������Լ�д�ĵ���¼�������onclick
            if(downP.x==curP.x && downP.y==curP.y){
            	   Log.i("liuwenjie", "====3=======\n");
                onSingleTouch();
                return true;
            }
        }

        return super.onTouchEvent(arg0);
    }

        /**
     * ����
     */
    public void onSingleTouch() {
        if (onSingleTouchListener!= null) {

            onSingleTouchListener.onSingleTouch();
        }
    }

    /**
     * ��������¼��ӿ�
     * @author wanpg
     *
     */
    public interface OnSingleTouchListener {
        public void onSingleTouch();
    }

    public void setOnSingleTouchListener(OnSingleTouchListener onSingleTouchListener) {
        this.onSingleTouchListener = onSingleTouchListener;
    }

}
